﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BluetoothMachine.ExpandHelper
{
    public static class ValueConversions
    {
        public static T UpdateValue<T>(this T t, T obj)
        {
            var objProper = obj.GetType().GetProperties();//客户端传过来的值
            foreach (var item in t.GetType().GetProperties())//前端传过来的值
            {
                objProper.Where(x => x.Name == item.Name).FirstOrDefault().SetValue(obj, item.GetValue(t, null));
            }
            return obj;
        }

        /// <summary>
        /// 红外数据解析
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static (byte[],short,short,bool) AnalysisInfrared(this byte[] bytes)
        {
            if(!((bytes[0] == 0x68) && (bytes[bytes.Length - 1] == 0x16))) return (new byte[0], 0, 0,false);
            short length = BitConverter.ToInt16(bytes.Skip(2).ToArray());
            try
            {
                return (bytes.Skip(4).Take(length).ToArray(),
                    BitConverter.ToInt16(bytes.Skip(bytes.Length - 5).ToArray()),
                    BitConverter.ToInt16(bytes.Skip(bytes.Length - 3).ToArray()), 
                    true);
            }
            catch (Exception)
            {
                return (new byte[0], 0, 0, false);
            }
        }
    }
}
